<script setup>
import { nextTick, onMounted, ref } from 'vue'
import { menuList, userGetMenu, userSetMenu } from '../../../api'
import PanelHead from '../../../components/panelHead.vue'
import { useRoute } from 'vue-router'

const route = useRoute()

onMounted(() => {
  userGetMenu().then(res => {
    // console.log(res)
    permissionData.value = res.data.data
  })
  getListData()
})

//列表数据
const tableData = ref({
  list: [],
  total: 0
})

//打开弹窗
const open = (rowData = {}) => {
  dialogFoemVisable.value = true
  //弹窗打开form生成是异步的
  nextTick(() => {
    if (rowData) {
      Object.assign(form.value, { id: rowData.id, name: rowData.name })
      treeRef.value.setCheckedKeys(rowData.permission)
    }
  })
}

const handleSizeChange = (value) => {
  paginationData.value.pageSize = value
  getListData()
}

const handleCurrentChange = (value) => {
  paginationData.value.pageNum = value
  getListData()
}

//请求列表数据
const getListData = () => {
  menuList(paginationData.value).then(res => {
    const { list, total } = res.data.data
    tableData.value.list = list
    tableData.value.total = total
  })
}
const paginationData = ref({
  pageNum: 1,
  pageSize: 10
})
//弹窗的显示隐藏
const dialogFoemVisable = ref(false)

//关闭弹窗的回调
const beforeClose = () => {
  dialogFoemVisable.value = false
  //重置表单
  formRef.value.resetFields()
  //tree选择重置
  treeRef.value.setCheckedKeys(defaultKeys)
}

//form 的数据
const form = ref({
  id: '',
  name: '',
  permissions: ''
})

const formRef = ref()
//树形菜单权限数据
const permissionData = ref([])

//选中权限
const defaultKeys = [4, 5]
const treeRef = ref()

const rules = ref({
  name: [{ required: true, trigger: 'blur', message: '请输入权限名称' }]
})
//表单提交
const confirm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      //获取到选择的checkbox数据
      const permissions = JSON.stringify(treeRef.value.getCheckedKeys())
      userSetMenu({ name: form.value.name, permissions, id: form.value.id }).then(res => {
        // console.log(res)
        beforeClose()
        getListData()
      })
    } else {
      console.log('error submit', fields)
    }
  })
}
</script>

<template>
  <PanelHead :route="route"></PanelHead>
  <div class="btns">
    <el-button :icon="Plus" @click="open(null)" type="primary" size="small">新增</el-button>
  </div>
  <el-table :data="tableData.list" style="width: 100%">
    <el-table-column prop="id" label="id"></el-table-column>
    <el-table-column prop="name" label="昵称"></el-table-column>
    <el-table-column prop="permissionName" label="菜单权限" width="500px"></el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="pagination-info">
    <el-pagination size="small" v-model:current-page="paginationData.pageNum" :page-size="paginationData.pageSize"
      :background="false" layout="total, prev, pager, next" :total="tableData.total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
  <el-dialog v-model="dialogFoemVisable" :before-close="beforeClose" title="添加权限" width="500px">
    <el-form ref="formRef" :rules="rules" label-width="100px" label-position="left" :model="form">
      <el-form-item prop="id" v-show="false">
        <el-input v-model="form.id"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请填写权限名称"></el-input>
      </el-form-item>
      <el-form-item label="权限" prop="permissions">
        <el-tree ref="treeRef" :data="permissionData" style="max-width: 600px" node-key="id" show-checkbox
          :default-checked-keys="defaultKeys" :default-expanded-keys="[2]"></el-tree>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm(formRef)">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="less">
.btns{
  padding: 10px 0 10px 10px; 
  background-color: #fff;
}
</style>